<template>
  <el-dialog :visible.sync="visible" v-if="visible" :title="title">
    <el-form :model="formData" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="广告名称">
        <el-input v-model="formData.name"  placeholder="请输入广告名称"/>
      </el-form-item>
      <el-form-item label="跳转连接">
        <el-input v-model="formData.href" placeholder="请输入跳转连接"/>
      </el-form-item>
      <el-form-item label="排序值" prop="orderNo">
        <el-input v-model="formData.orderNo" placeholder="请输入排序值，值越大越靠前"/>
      </el-form-item>
      <el-form-item label="跳转方式">
        <el-select style="width: 100%" v-model="formData.target" clearable filterable placeholder="跳转方式">
          <el-option label="当前页" value="_self"/>
          <el-option label="新页面" value="_blank"/>
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="expiresStart">
        <el-date-picker
          v-model="formData.expiresStart"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="expiresEnd">
        <el-date-picker
          v-model="formData.expiresEnd"
          type="datetime"
          placeholder="选择日期时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="广告图片">
        <el-upload :action="uploadUrl" :headers="uploadHeader" :limit="1" :file-list="fileList" list-type="picture-card" ref="ad" :on-success="onSuccess" :before-upload="beforeUpload" :on-exceed="onExceed">
          <i class="el-icon-plus"></i>
          <div slot="file" slot-scope="{file}" style="height: 100%;width: 100%">
            <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
            <div class="el-upload-list__item-actions">
              <span @click="showImage(file)"><i class="el-icon-zoom-in"></i></span>
              <span @click="removeImage(file)"><i class="el-icon-delete"></i></span>
            </div>
          </div>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态">
        <el-radio-group v-model="formData.status">
          <el-radio :label="1">可用</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button type="warning" @click="visible = false">取消</el-button>
      </el-form-item>
    </el-form>
    <el-dialog :visible.sync="imageDialog" title="查看图片" v-if="imageDialog" append-to-body>
      <a :href="imageSrc" target="_blank" title="点击查看原图" style="display: block;height: 100%;width: 100%">
        <img :src="imageSrc" alt="" style="display: block;height: 100%;width: 100%">
      </a>
    </el-dialog>
  </el-dialog>
</template>

<script>
const formData = {
  id: '',
  name: '',
  src: '',
  href: '',
  target: '_self',
  orderNo: 0,
  expiresStart: '',
  expiresEnd: '',
  status: 1
}
export default {
  name: 'AdUpdate',
  data () {
    return {
      visible: false,
      title: '添加数据',
      formData: {
        ...formData
      },
      rules: {
        orderNo: [{
          required: true,
          message: '请输入排序值'
        }],
        expiresStart: [{
          required: true,
          message: '请选择开始时间'
        }],
        expiresEnd: [{
          required: true,
          message: '请选择结束时间'
        }]
      },
      baseImage: process.env.VUE_APP_URL,
      uploadUrl: process.env.VUE_APP_URL + 'upload',
      uploadHeader: {
        token: this.$cookies.get('token')
      },
      fileList: [],
      imageDialog: false,
      imageSrc: '',
      info: false
    }
  },
  methods: {
    init (id) {
      this.visible = true
      this.title = id ? '修改数据' : '添加数据'
      this.formData = { ...formData }
      this.fileList.length = 0
      if (id) {
        this.$http.get(`ad/info/${id}`).then(data => {
          if (data) {
            this.formData = { ...data }
            this.fileList.push({ url: this.baseImage + data.src })
          }
        })
      }
    },
    onExceed () {
      this.$message.error('超出个数限制！')
    },
    beforeUpload (file) {
      const isImage = file.type === 'image/jpeg' || file.type === 'image/jpg' || file.type === 'image/png' || file.type === 'image/gif'
      if (!isImage) {
        this.$message.error('请上传图片，格式支持：jpg,jpeg,png,gif')
      }
      const isLt20M = (file.size / 1024 / 1024) < 20
      if (!isLt20M) {
        this.$message.error('图片大小不能超过20MB')
      }
      return isImage && isLt20M
    },
    onSuccess (res) {
      if (res.code === 200) {
        this.formData.src = res.data
      } else {
        this.$message.error(`上传异常:{${res.msg}}`)
      }
    },
    removeImage (file) {
      this.$confirm('确定删除该图片吗?', '询问', { type: 'info' }).then(() => {
        this.formData.src = ''
        this.$refs.ad.handleRemove(file)
      }).catch(() => {})
    },
    showImage (file) {
      this.imageDialog = true
      this.imageSrc = file.url
    },
    submit () {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http.post('ad/update', this.formData).then(() => {
            this.$message.success('操作成功')
            this.visible = false
            this.$emit('closeDialog')
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
